package com.siao.display
{
	import com.siao.events.DragEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	[Event(name="start", type="com.siao.events.DragEvent")]
	
	[Event(name="move", type="com.siao.events.DragEvent")]
	
	[Event(name="complete", type="com.siao.events.DragEvent")]
	
	/**
	 * 可拖拽显示元素
	 * @author SiaoLeon
	 */
	public class DragElement extends LayoutElement
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var localX:Number;
		
		private var localY:Number;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function DragElement()
		{
			super();
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		// -------------------------------------------------------------------------------------------------------- 函数
		
		override protected final function init():void
		{
			super.init();
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler, false, 0, true);
		}
		
		override protected final function exit():void
		{
			super.exit();
			
			this.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			
			if (this.stage != null)
			{
				this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
				this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
				this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseUpHandler);
			}
		}
		
		/**
		 * 鼠标按下时
		 * @param event
		 */
		private function onMouseDownHandler(event:MouseEvent):void
		{
			this.alignH = Align.NONE;
			this.alignV = Align.NONE;
			
			this.parent.setChildIndex(this, this.parent.numChildren - 1);
			
			this.localX = this.mouseX * this.scaleX;
			this.localY = this.mouseY * this.scaleY;
			
			this.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler, false, 0, true);
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler, false, 0, true);
			this.stage.addEventListener(Event.MOUSE_LEAVE, onMouseUpHandler, false, 0, true);
			
			this.dispatchEvent(new DragEvent(DragEvent.START));
		}
		
		/**
		 * 鼠标移动时
		 * @param event
		 */
		private function onMouseMoveHandler(event:MouseEvent):void
		{
			var x:Number = this.parent.mouseX - this.localX;
			var y:Number = this.parent.mouseY - this.localY;
			
			var meta:Point = this.parent.localToGlobal(new Point(0, 0));
			
			this.x = (x < meta.x * -1) ? meta.x * -1 : (x > meta.x * -1 + this.stage.stageWidth - this.width) ? meta.x * -1 + this.stage.stageWidth - this.width : x;
			this.y = (y < meta.y * -1) ? meta.y * -1 : (y > meta.y * -1 + this.stage.stageHeight - this.height) ? meta.y * -1 + this.stage.stageHeight - this.height : y;
			
			event.updateAfterEvent();
			
			this.dispatchEvent(new DragEvent(DragEvent.MOVE));
		}
		
		/**
		 * 鼠标抬起时
		 * @param event
		 */
		private function onMouseUpHandler(event:Event):void
		{
			this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
			this.stage.removeEventListener(Event.MOUSE_LEAVE, onMouseUpHandler);
			
			this.dispatchEvent(new DragEvent(DragEvent.COMPLETE));
		}
	
	}
}
